package com.tree.common.controller;

/**
 * @ClassName com.demo.common.base
 * Description: <类功能描述>. <br>
 * <p>
 * base controller， 实现增删改查
 * </p>
 * @Author tree
 * @Date 2019-12-15 09:35
 * @Version 1.0
 */
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.tree.common.dto.Result;
import com.tree.common.enums.ResultCode;
import com.tree.common.util.Log;
import com.tree.common.entity.BaseEntity;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;


@Slf4j
public class BaseController<S extends IService<E>, E extends BaseEntity> {

    @Autowired
    private S service;

    @GetMapping("/list")
    @ApiOperation(value = "查询列表", notes = "查询列表", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Result<IPage<E>> list(@RequestBody E entity){
        log.debug(Log.op("list").kv("entity", JSON.toJSONString(entity))
                .msg("开始查询").toString());
        IPage<E> page = service.page(new Page<E>(entity.getPage(),entity.getPageSize()), new QueryWrapper<>(entity));
        log.debug(Log.op("list").kv("result", JSON.toJSONString(page))
                .msg("查询成功").toString());
        return Result.success(page);
    }

    @GetMapping("/get/{id}")
    @ApiOperation(value = "根据ID查询", notes = "根据ID查询", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Result<E> get(@PathVariable Long id){
        log.debug(Log.op("get").kv("id", id)
                .msg("开始查询").toString());
        if(id == null){
            return Result.fail(ResultCode.PARAM_IS_INVALID.getCode(),"ID为空");
        }
        E result = service.getById(id);
        log.debug(Log.op("list").kv("result", JSON.toJSONString(result))
                .msg("查询成功").toString());
        return Result.success(result);

//        return new FrontResponse<E>().setCode(SUCCESS)
//                .setMessage("查询成功").setObject(result);
    }


    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "根据Id删除记录", notes = "根据Id删除记录", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Result<Boolean> delete(@PathVariable Long id){
        log.debug(Log.op("delete").kv("id", id)
                .msg("开始删除").toString());
        if(id == null){
            return Result.fail(ResultCode.PARAM_IS_INVALID.getCode(),"ID为空");
        }
        boolean result = service.removeById(id);
        log.debug(Log.op("list").kv("result", JSON.toJSONString(result))
                .msg("删除成功").toString());
        return Result.success(result);
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增记录", notes = "新增记录", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Result<Boolean> add(@RequestBody E entity){
        log.debug(Log.op("add").kv("entity", JSON.toJSONString(entity))
                .msg("开始新增").toString());
        boolean result = service.save(entity);
        log.debug(Log.op("list").kv("result", JSON.toJSONString(result))
                .msg("新增记录成功").toString());
        return Result.success(result);
    }

    @PutMapping("/update")
    @ApiOperation(value = "更新记录", notes = "新增记录", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Result<Boolean> update(@RequestBody E entity){
        log.debug(Log.op("add").kv("entity", JSON.toJSONString(entity))
                .msg("开始更新").toString());
        if(entity.getId() == null){
            return Result.fail(ResultCode.PARAM_IS_INVALID.getCode(),"ID为空");
        }
        boolean result = service.updateById(entity);
        log.debug(Log.op("list").kv("result", JSON.toJSONString(result))
                .msg("更新记录成功").toString());
        return Result.success(result);
    }

}
